/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vce.web.servlet;

import com.vce.BusinessException;
import com.vce.election.Election;
import com.vce.election.domain.Jrv;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Ggutierrez
 */
public class DocumentValidateServlet extends HttpServlet {

    private Election election;
    private ApplicationContext appContext;

    static class Response {

        boolean success;

        public Response() {
            success = true;
        }

        public Response(boolean success, String message) {
            this.success = success;
        }
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     */
    protected void activate(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (appContext == null) {
            appContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            election = (Election) appContext.getBean("election");
        }
        response.setContentType("application/json");
        response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
        response.setHeader("Pragma", "no-cache"); //HTTP 1.0
        response.setDateHeader("Expires", 0); //prevents caching at the proxy server

        String field = request.getParameter("field");
        String operation = request.getParameter("operation");
        Response resp = null;

        if (field.equals("autocomplete_jrv")) {
            Long jrvId = Long.valueOf(request.getParameter("id"));
            Jrv jrv = election.getJrv(jrvId);

            if (jrv == null) {
                resp = new Response(false, "Record not found");
                response.getWriter().append("{");
                response.getWriter().append("\"success jrv\":" + (jrv != null ? "\"OK\"" : "\"FAIL\""));
                response.getWriter().append("}");
                response.getWriter().flush();
                response.getWriter().close();
            } else {                

                try {
                    response.getWriter().append("{");
                    response.getWriter().append("\"success\":" + (jrv != null ? "\"OK\"" : "\"FAIL\"") + ",");
                    response.getWriter().append("\"junta\":{" + "\"id\":" + (jrv.getId()) + "," + "\"code\":" + "\"" + jrv.getCode() + "\"" + ","
                            + "\"ballotId\":" + "\"" + jrv.getBallotCentre().getId() + "\"" + ","
                            + "\"townId\":" + "\"" + jrv.getBallotCentre().getTown().getId() + "\"" + ","
                            + "\"townName\":" + "\"" + jrv.getBallotCentre().getTown().getName() + "\"" + ","
                            + "\"ballotDesc\":" + "\"" + jrv.getBallotCentre().getDescription() + "\"" + ","
                            + "\"operation\":" + "\"" + operation + "\"" + "}");

                    response.getWriter().append("}");
                    response.getWriter().flush();
                    response.getWriter().close();
                } catch (BusinessException e) {
                    resp = new Response(false, e.getMessage());
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        activate(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        activate(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
